package com.shareppy.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class DateUtils {

	public static final long SECOND = 1000;
	public static final long MINUTE = 60000;
	public static final String YYYYMMDDHH24MMSS = "yyyyMMddHHmmss";
	public static final String YYYYMMDDHHMMSS = "yyyyMMddhhmmss";
	public static final String YYYYMMDD = "yyyyMMdd";

	public static final String YYYY_MM_DD_HH24_MM = "yyyy/MM/dd HH:mm";
	public static final String YYYY_MM_DD_HH_MM = "yyyy/MM/dd hh:mm";

	private static final Map<String, SimpleDateFormat> FORMATTERS = new HashMap<String, SimpleDateFormat>();

	private DateUtils() {
		super();
	}

	public static Date toDate(String date) {
		return toDate(date, YYYYMMDDHH24MMSS);
	}

	public static Date toDate(String date, String format) {
		if (date != null) {
			try {
				return getFormatter(format).parse(date);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static String toString(Date date) {
		return toString(date, YYYYMMDDHH24MMSS);
	}

	public static String toString(Date date, String format) {
		return getFormatter(format).format(date);
	}

	private static SimpleDateFormat getFormatter(String format) {
		SimpleDateFormat formatter = FORMATTERS.get(format);
		if (formatter == null) {
			formatter = new SimpleDateFormat(format, Locale.getDefault());
			FORMATTERS.put(format, formatter);
		}
		return formatter;
	}

	public static long toLong() {
		return toLong(new Date());
	}

	public static long toLong(Date date) {
		return Long.parseLong(toString(date));
	}

	public static int toInt() {
		return toInt(new Date());
	}

	public static int toInt(Date date) {
		return Integer.parseInt(toString(date, YYYYMMDD));
	}

	public static String toReadableTime(long millis) {
		if (millis < SECOND) {
			return millis + " milliseconds";
		}
		if (millis < MINUTE) {
			return (millis / (float) SECOND) + " seconds";
		}
		return (millis / (float) MINUTE) + " minutes";
	}
}